package com.project.music.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.project.music.bean.Song;
import com.project.music.service.SongService;
import com.project.music.utils.Consts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 歌曲控制类
 */

@Api(tags = "歌曲控制类")
@RestController
@RequestMapping("/song")
public class SongController {

    @Autowired
    private SongService songService;

    //返回所有歌曲
    @ApiOperation("返回所有歌曲")
    @GetMapping("")
    public Object allSong() {
        return songService.list();
    }
    //添加歌曲
    @ApiOperation("添加歌曲")
    @PostMapping("/add")
    public JSONObject addSong(@RequestParam("singerId") @ApiParam("歌手ID") String singer_id,
                          @RequestParam("name") @ApiParam("歌名") String name,
                          @RequestParam("introduction") @ApiParam("简介") String introduction,
                          @RequestParam("lyric") @ApiParam("歌词") String lyric,
                          @RequestParam("file") @ApiParam("歌曲文件") MultipartFile file) {
        String pic = "/img/songPic/tubiao.jpg";

        JSONObject jsonObject = new JSONObject();
        if (file.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "音乐上传失败");
            return jsonObject;
        }

        String fileName = file.getOriginalFilename();
        String filePath = Consts.ROOT + Consts.SEPARATOR + "song";
        File path = new File(filePath);
        if (!path.exists()) {
            path.mkdir();
        }

        File dest = new File(filePath + Consts.SEPARATOR + fileName);
        String storeUrlPath = "/song/" + fileName;
        try {
            file.transferTo(dest);
            Song song = new Song();
            song.setSingerId(Integer.parseInt(singer_id));
            song.setName(name);
            song.setIntroduction(introduction);
            song.setCreate_time(new Date());
            song.setUpdate_time(new Date());
            song.setPic(pic);
            song.setLyric(lyric);
            song.setUrl(storeUrlPath);
            boolean status = songService.save(song);
            if (status) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "上传成功");
            }
            else {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "上传失败");
            }
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "上传失败" + e.getMessage());
            return jsonObject;
        }
    }
    //指定ID返回歌曲
    @ApiOperation("指定ID返回歌曲")
    @GetMapping("/detail")
    public Song selectSongById(@RequestParam("id") @ApiParam("歌曲ID") int id) {
        return songService.getById(id);
    }
    //返回指定歌手ID的歌曲
    @ApiOperation("返回指定歌手ID的歌曲")
    @GetMapping("/singer/detail")
    public List<Song> selectSongBySingerId(@RequestParam("singerId") @ApiParam("歌手ID") int singerId) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Song::getSingerId, singerId);
        return songService.list(queryWrapper);
    }
    //返回指定歌手名的歌曲
    @ApiOperation("返回指定歌手名的歌曲")
    @GetMapping("/singerName/detail")
    public List<Song> songOfSingerName(@RequestParam("name") @ApiParam("歌手名") String name) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .like(Song::getName, name);
        return songService.list(queryWrapper);
    }
    //返回指定歌曲名的歌曲
    @ApiOperation("返回指定歌曲名的歌曲")
    @GetMapping("name/detail")
    public List<Song> songOfName(@RequestParam("name") @ApiParam("歌曲名") String name) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Song::getName, name);
        return songService.list(queryWrapper);
    }
    //删除歌曲
    @ApiOperation("删除歌曲")
    @GetMapping("/delete")
    public Boolean deleteSong(@RequestParam("id") @ApiParam("歌曲ID") int id) {
        Song song = songService.getById(id);
        String filePath = Consts.ROOT + song.getUrl();
        File musicFile = new File(filePath);
        boolean musicDelete = musicFile.delete();

        if (!song.getPic().equals("/img/songPic/tubiao.jpg")) {
            String picPath = Consts.ROOT + song.getPic();
            File picFile = new File(picPath);
            boolean picDelete = picFile.delete();
            return songService.removeById(id) && musicDelete && picDelete;
        } else {
            return songService.removeById(id) && musicDelete;
        }
    }
    //更新歌曲信息
    @ApiOperation("更新歌曲信息")
    @PostMapping("/update")
    public JSONObject updateSongMsg(Song song) {
        JSONObject jsonObject = new JSONObject();
        boolean status = songService.updateById(song);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "上传成功");
        }
        else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "上传失败");
        }
        return jsonObject;
    }
    //更新歌曲图片
    @ApiOperation("更新歌曲图片")
    @PostMapping("/img/update")
    public JSONObject updateSongPic(@RequestParam("id") @ApiParam("歌曲ID") int id,
                                @RequestParam("file") @ApiParam("歌曲文件") MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        if (file.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "图片上传失败");
            return jsonObject;
        }

        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = Consts.ROOT + Consts.SEPARATOR + "img" + Consts.SEPARATOR + "songPic";
        File path = new File(filePath);
        if (!path.exists()) {
            path.mkdir();
        }

        File dest = new File(filePath + Consts.SEPARATOR + fileName);
        String storePath = "/img/songPic/" + fileName;
        try {
            file.transferTo(dest);
            UpdateWrapper<Song> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                         .eq(Song::getId, id)
                         .set(Song::getPic, storePath);
            boolean status = songService.update(updateWrapper);
            if (status) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "上传成功");
            }
            else {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "上传失败");
            }
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败" + e.getMessage());
            return jsonObject;
        }
    }
    //更新歌曲文件
    @ApiOperation("更新歌曲文件")
    @PostMapping("/url/update")
    public JSONObject updateSongUrl(@RequestParam("id") @ApiParam("歌曲ID") int id,
                                @RequestParam("file") @ApiParam("歌曲文件") MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        if (file.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "音乐上传失败");
            return jsonObject;
        }

        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = Consts.ROOT + Consts.SEPARATOR + "song";
        File path = new File(filePath);
        if (!path.exists()) {
            path.mkdir();
        }

        File dest = new File(filePath + Consts.SEPARATOR + fileName);
        String storePath = "/song/" + fileName;
        try {
            file.transferTo(dest);
            UpdateWrapper<Song> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .eq(Song::getId, id)
                    .set(Song::getUrl, storePath);
            boolean status = songService.update(updateWrapper);
            if (status) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "上传成功");
            }
            else {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "上传失败");
            }
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败" + e.getMessage());
            return jsonObject;
        }
    }
}
